package no.uia.ikt.gateway;

import java.awt.Component;

import org.cybergarage.upnp.Action;
import org.cybergarage.upnp.ArgumentList;
import org.cybergarage.upnp.ControlPoint;
import org.cybergarage.upnp.Device;
import org.cybergarage.upnp.Service;
import org.cybergarage.upnp.StateVariable;
import org.cybergarage.upnp.device.InvalidDescriptionException;
import org.cybergarage.upnp.device.NotifyListener;
import org.cybergarage.upnp.device.SearchResponseListener;
import org.cybergarage.upnp.event.EventListener;
import org.cybergarage.upnp.ssdp.SSDPPacket;

public class Gateway2 extends ControlPoint implements NotifyListener,
		EventListener, SearchResponseListener {
	
	private final static String DESCRIPTION_FILE_NAME = "description/description.xml";

	private final static String DISPLAY_DEVICE_TYPE = "urn:schemas-upnp-org:device:display:1";
	private final static String DISPLAY_SERVICE_TYPE = "urn:schemas-upnp-org:service:power:1";

	private SSDPPacket ssdpPacket;
	private Device gatewayDev;
	private ControlPoint ctrlPoint;

	public Gateway2() {
		try {
			gatewayDev = new Device(DESCRIPTION_FILE_NAME);
		} catch (InvalidDescriptionException e) {
			e.printStackTrace();
		}
		ctrlPoint = new ControlPoint();

		ctrlPoint.addNotifyListener(this);
		ctrlPoint.addSearchResponseListener(this);
		ctrlPoint.addEventListener(this);

		addNotifyListener(this);
		addSearchResponseListener(this);
		addEventListener(this);
		search();
	}

	// add by xiaobo
	private StateVariable msgVar;
	
	public void display(String[] str) {
		msgVar = gatewayDev.getStateVariable("Message");
		System.out.println("msgVar is " + msgVar.toString());
		msgVar.setValue(str[5]);
		System.out.println("The value is " + str[5]);
	}

	public Service getDeviceService(String deviceType, String serviceType) {
		Device dev = getDevice(deviceType);
		if (dev == null) {
			return null;
		}
		Service service = dev.getService(serviceType);
		if (service == null) {
			return null;
		}
		return service;
	}

	public boolean subscribeService(SSDPPacket packet, String deviceType,
			String serviceType) {
		Service service = getDeviceService(deviceType, serviceType);
		if (service == null) {
			return false;
		}
		return subscribe(service);
	}

	public void checkNewDevices(SSDPPacket packet) {
		subscribeService(packet, DISPLAY_DEVICE_TYPE, DISPLAY_SERVICE_TYPE);
	}

	public void checkRemoveDevices(SSDPPacket packet) {
		if (isDevice(packet, DISPLAY_DEVICE_TYPE) == true) {
			System.out.println("Airconditionor is removed!");
		}
	}

	public boolean isDevice(SSDPPacket packet, String deviceType) {
		String usn = packet.getUSN();
		if (usn.endsWith(deviceType)) {
			return true;
		}
		return false;
	}

	public void deviceNotifyReceived(SSDPPacket packet) {
		if (packet.isAlive() == true) {
			checkNewDevices(packet);
		}
		if (packet.isByeBye() == true) {
			checkRemoveDevices(packet);
		}
	}

	public void deviceSearchResponseReceived(SSDPPacket packet) {
	}

	private String message = "";
	private Component comp;

	public void eventNotifyReceived(String uuid, long seq, String name,
			String value) {
	}

	public void powerOn(String deviceType) {
		Device dev = getDevice(deviceType);
		if (dev == null) {
			return;
		}
		Action getPowerAct = dev.getAction("GetPower");
		if (getPowerAct.postControlAction() == false) {
			return;
		}

		ArgumentList outArgList = getPowerAct.getOutputArgumentList();
		String powerState = outArgList.getArgument(0).getValue();
		String newPowerState = (powerState.compareTo("1") == 0) ? "0" : "1";

		Action setPowerAct = dev.getAction("SetPower");
		setPowerAct.setArgumentValue("Power", newPowerState);
		setPowerAct.postControlAction();
	}

	public Component getComp() {
		return comp;
	}

	public void setComp(Component comp) {
		this.comp = comp;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public SSDPPacket getSsdpPacket() {
		return ssdpPacket;
	}

	public void setSsdpPacket(SSDPPacket ssdpPacket) {
		this.ssdpPacket = ssdpPacket;
	}
}
